package me.chan.chatting.server.handler;

import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import me.chan.chatting.server.msg.PingMessage;


public class MyChannelDuplexHandler extends ChannelDuplexHandler {

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

        IdleStateEvent event = (IdleStateEvent) evt;
        if (event.state() == IdleState.READER_IDLE) {
            ctx.channel().close();
        }

        if (event.state() == IdleState.WRITER_IDLE) {
            ctx.writeAndFlush(new PingMessage());
        }
    }
}
